package com.leftheart.controller;


import cn.hutool.core.date.DateUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.leftheart.entity.Rental;
import com.leftheart.service.RentalService;
import com.leftheart.utils.DateUtils;
import com.leftheart.utils.Utils;
import com.sun.media.sound.SoftVoice;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("rental")
public class RentalController {

//    private static String rentalName;
//    private static String rentalUserName;

    @Autowired
    RentalService rentalService;

//  查询全部租借用户
    @GetMapping("/queryAll")
    public String rentalList(Model model){
        List<Rental> rentals = rentalService.queryAllRental();
        model.addAttribute("rentals",rentals);
        return "rental/rentallist";
    }

//  重定向到rentallist页面
    @RequestMapping("/comlist")
    public String toRentalList(){
        return "redirect:/rentail/querylist";
    }

//  前往添加租借用户页面
    @RequestMapping("/toAddRental")
    public String toAddRental(){
        return "rental/addrental";
    }

//
//    @PutMapping("/addRental")
//    public String addRental(Rental rental,@PathVariable("rentalName") String rentalName,@PathVariable("rentalUserName") String rentalUserName){
//        rental.setRentalName(rentalName);
//        rental.setRentalUserName(rentalUserName);
//        return "redirect:/rental/addRental2";
//    }
    @PostMapping("/addRental")
    public String addRental2(HttpServletRequest request,Rental rental,@PathVariable("rentalName") String rentalName,@PathVariable("rentalUserName") String rentalUserName){
        rental.setRentalId();
        rental.setRentalName(request.getParameter(rentalName));
        rental.setRentalUserName(request.getParameter(rentalUserName));
        rental.setRentalUserId(rentalService.queryIdByName(rentalUserName));
        rentalService.addRental(rental);
        return "redirect:/rental/queryAll";
    }


//    @Test
//    public void test2(Rental rental){
//        rental.setRentalId(Utils.setRentalId());
//    }
//    @Test
//    public void test(){
//        System.out.println(rentalService.queryIdByName("lwh"));
//    }

}
